﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;

namespace RH
{
   public partial class wfPrestamoMnt : EditPage
   {
      public const int _GRDLIST_STCUOTA = 11;
      public const int _GRDLIST_STANULADO = 12;
      
      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
         this.SetInitialTitle("Prestamo");
      }

      public override void LoadListControls()
      {
         ETablaGenDet objEDet = new ETablaGenDet();
         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

         objEDet.IdTabla = ConstMultitabla.TIPO_MONEDA;
         DataTable dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidMoneda, dt, "dsDescrip", "idCodigo", true, false);

         objEDet.IdTabla = ConstMultitabla.TIPO_PRESTAMO;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidTipoPrest, dt, "dsDescrip", "idCodigo", true, false);

         //objEDet.IdTabla = ConstMultitabla.TIPO_CANCEL_PRESTAMO;
         //dt = objBOI.GetDetailByList(objEDet);
         //WebHelper.LoadListControl(PddlidTipoCance, dt, "dsDescrip", "idCodigo", true, false);

         EConcepRemu objECR = new EConcepRemu();
         RHBOQry.IConcepRemu objBOCR = (RHBOQry.IConcepRemu)WCFHelper.GetObject<RHBOQry.IConcepRemu>(typeof(RHBOQry.ConcepRemu));
         objECR.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objECR.IdTipConcep = ConstTipoConcepPlanilla.RETENCION;
         dt = objBOCR.GetListXTipConce(objECR);
         WebHelper.LoadListControl(ddlidConRemu, dt, "dsConRemu", "idConRemu", true, false);

         this.hdnstCalcular.Value = ConstFlagEstado.DESACTIVADO;
         this.hdnstNuProceCal.Value = ConstFlagEstado.DESACTIVADO;

         if (this.FormEditAction == FormEditAction.AddNew)
            this.txtfePrestamo.Text = DateTime.Now.ToString();
      }

      public override void DataToControls()
      {
         EPrestamo objE = new EPrestamo();
         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Prestamo));
         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objE.IdPersonal = Request.QueryString["IdPersonal"];
         objE.IdPrestamo = Request.QueryString["IdPrestamo"];
         objE = (EPrestamo)objBO.GetByKey(objE);

         if (objE != null)
         {
            WebHelper.EntityToControl(objE, "IdPersonal", hdnidPersonal);
            
            EPersonal objEPer = new EPersonal();
            RHBOQry.IPersonal objBOPer = (RHBOQry.IPersonal)WCFHelper.GetObject<RHBOQry.IPersonal>(typeof(RHBOQry.Personal));
            objEPer.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objEPer.IdPersonal = objE.IdPersonal;
            DataTable dtPer = objBOPer.GetCliProvXPersonal(objEPer);
            if (dtPer.Rows.Count > 0)
               this.txtidPersonal.Text = dtPer.Rows[0]["dsRazSocial"].ToString();
            
            WebHelper.EntityToControl(objE, "IdPrestamo", txtidPrestamo);
            WebHelper.EntityToControl(objE, "IdConRemu", ddlidConRemu);
            WebHelper.EntityToControl(objE, "FePrestamo", txtfePrestamo);
            WebHelper.EntityToControl(objE, "IdMoneda", ddlidMoneda);
            WebHelper.EntityToControl(objE, "MtTC", txtmtTC);
            WebHelper.EntityToControl(objE, "MtPrestamo", txtmtprestamo);
            WebHelper.EntityToControl(objE, "QtCuotas", txtqtcuotas);
            WebHelper.EntityToControl(objE, "IdPlanilla", hdnidPlanilla);
            WebHelper.EntityToControl(objE, "IdForPago", hdnidForPago);
            WebHelper.EntityToControl(objE, "DsObserva", txtdsObservacion);

            EPlanilla objEPl = new EPlanilla();
            IBOQuery objBOPl = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Planilla)); 
            objEPl.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objEPl.IdPlanilla = objE.IdPlanilla;

            objEPl = (EPlanilla)objBOPl.GetByKey(objEPl);
            if (objEPl!=null)
            WebHelper.EntityToControl(objEPl, "DsPlanilla", txtidPlanilla);

            WebHelper.EntityToControl(objE, "NuProcAnual", txtnuProcAnual);
            WebHelper.EntityToControl(objE, "IdTipoPrest", ddlidTipoPrest);


            ETablaGenDet objETDet = new ETablaGenDet();
            MGBOQry.ITablaGen objBOTDet = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
            objETDet.IdTabla = ConstMultitabla.FORMA_PAGO;
            objETDet.IdCodigo = objE.IdForPago;
            objETDet = (ETablaGenDet)objBOTDet.GetDetailByKey(objETDet);
            if(objETDet!= null)
               WebHelper.EntityToControl(objETDet, "DsDescrip",txtidForPago);

         }

         EPrestaCuota objEDet = new EPrestaCuota();
         RHBOQry.IPrestamo objBODet = (RHBOQry.IPrestamo)WCFHelper.GetObject<RHBOQry.IPrestamo>(typeof(RHBOQry.Prestamo));
         objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objEDet.IdPersonal = Request.QueryString["IdPersonal"];
         objEDet.IdPrestamo = Request.QueryString["IdPrestamo"];
         DataTable dt = objBODet.GetByParentKeyPrestaCuota(objEDet);
         this.GridSetDataBinding(grdList, dt);

      }

      public override void RegisterGeneralScripts()
      {
         this.txtidPrestamo.Attributes.Add("readonly", "true");
         this.txtidPlanilla.Attributes.Add("readonly", "true");
         this.txtidForPago.Attributes.Add("readonly", "true");
         this.txtnuProcAnual.Attributes.Add("readonly", "true");
         this.txtmtTC.Attributes.Add("readonly", "true");
         //this.txtdsObservacion.Attributes.Add("readonly", "true");

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("var varRet = modalDialog('wfPeriodoPlani.aspx?IdPersonal=' + document.getElementById('" + this.hdnidPersonal.ClientID + "').value,750,400);");
         sb.AppendLine("if(varRet==null)");
         sb.AppendLine("return false;");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidForPago.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtidForPago.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[1];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPlanilla.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[2];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtidPlanilla.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[3];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtnuProcAnual.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[4];");

         this.txtidPlanilla.OnClientClick = sb.ToString();

         sb = new StringBuilder();

         sb.AppendLine("var varRet = modalDialog('wfPeriodoPlani.aspx?IdPersonal=' + document.getElementById('" + this.hdnidPersonal.ClientID + "').value,750,400);");
         sb.AppendLine("if(varRet==null)");
         sb.AppendLine("return false;");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidForPagoPp.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtidForPagoPp.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[1];");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPlanillaPp.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[2];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtidPlanillaPp.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[3];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtnuProcAnualPp.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[4];");

         this.txtidPlanillaPp.OnClientClick = sb.ToString();

         sb = new StringBuilder();
         sb.AppendLine("var varRet = modalDialog('wfPersonal.aspx',750,400);");
         sb.AppendLine("if(varRet==null)");
         sb.AppendLine("return false;");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPersonal.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtidPersonal.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[1];");
         this.txtidPersonal.OnClientClick = sb.ToString();

         sb = new StringBuilder();

         sb.AppendLine("function validateEditForm(key){");
         sb.AppendLine("var varRpta = isEmptyControl('" + this.txtidPersonal.ClientID + "','Ingrese el Personal');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidConRemu.ClientID + "','Seleccione el Concepto Remunerativo');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("var varRpta = isEmptyControl('" + this.txtfePrestamo.ClientID + "','Ingrese la fecha de Prestamo');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidMoneda.ClientID + "','Seleccione');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtmtprestamo.ClientID + "','Ingrese el monto del prestamo.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtqtcuotas.ClientID + "','Ingrese la cantidad de cuotas.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtidPlanilla.ClientID + "','Seleccione la planilla.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuProcAnual.ClientID + "','Ingrese el Nro de Proceso Inicial.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("if(document.getElementById('" + this.hdnstCalcular.ClientID + "').value == '" + ConstFlagEstado.ACTIVADO + "'){");
         sb.AppendLine("alert('Se han cambiado algunos datos, recalcule el cronograma de pago.');return false;}");
         sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");

         sb.AppendLine("function validateEditFormGrid(key){");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtmtprestamo.ClientID + "','Ingrese el monto del prestamo.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtqtcuotas.ClientID + "','Ingrese la cantidad de cuotas.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtidPlanilla.ClientID + "','Seleccione la planilla.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtnuProcAnual.ClientID + "','Ingrese el Nro de Proceso Inicial.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("menu_doPostBack('" + mnuGrid.ClientID + "','" + mnuGrid.UniqueID + "',key);");
         sb.AppendLine("}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
      }

      public void RegisterEditPopupScripts()
      {

         this.txtidForPagoPp.Attributes.Add("readonly", "true");
         this.txtnuCuotaPp.Attributes.Add("readonly", "true");
         this.txtnuProcAnualPp.Attributes.Add("readonly", "true");

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("function validate" + this.bttOkPopup.ID + "(){");
         //sb.AppendLine("varRpta = isEmptyControl('" + this.PddlidTipoCance.ClientID + "','Ingrese el tipo de Cancelacion.');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");
         //sb.AppendLine("varRpta = isEmptyControl('" + this.PtxtmtCuota.ClientID + "','Ingrese el monto de la cuota.');");
         //sb.AppendLine("if(varRpta==true)");
         //sb.AppendLine("return !varRpta;");
         sb.AppendLine("return true;}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "validatePopup", sb.ToString(), true);

         this.bttOkPopup.OnClientClick = "return validate" + bttOkPopup.ID + "();";
         this.bttCancelPopup.OnClientClick = "$find('" + this.mdlPopup.ClientID + "').hide();return false;";

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if (this.FormEditAction != FormEditAction.Delete)
                  this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break;
            case ConstMenuToolbarValue.BUTTON_EXIT:
               this.Response.Redirect("wfPrestamoBnd.aspx");
               break;
         }
      }

      protected void mnuGrid_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_CALCULAR:
               this.CalcularCuotas();
               this.RegisterEditPopupScripts();
               this.hdnstCalcular.Value = ConstFlagEstado.DESACTIVADO;
               this.hdnstNuProceCal.Value = ConstFlagEstado.DESACTIVADO;
               break;
         }

      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);

         switch (e.CommandName)
         {
            case ConstGridRowCommand.GRIDEDIT:
               this.lblTitlePopup.Text = "Prestamo (Editar - cuota)";
               this.GridEditAction = GridEditAction.Edit;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
         }
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
            String strRI = e.Row.RowIndex.ToString();

            ImageButton bttedit = (ImageButton)e.Row.FindControl("grdBtt" + ConstGridRowCommand.GRIDEDIT);
            ImageButton bttdelete = (ImageButton)e.Row.FindControl("grdBtt" + ConstGridRowCommand.GRIDDELETE);

            bttedit.OnClientClick = "javascript:return validateGridCommands('" + ConstGridRowCommand.GRIDEDIT + "','" + strRI + "');";
            bttdelete.OnClientClick = "javascript:return validateGridCommands('" + ConstGridRowCommand.GRIDDELETE + "','" + strRI + "');";

            if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
            {
               e.Row.ForeColor = System.Drawing.Color.Red;
               bttedit.ImageUrl = "../Images/Icons/Disabled/Edit.png";
               bttdelete.ImageUrl = "../Images/Icons/Disabled/Delete.png";
            }
            else
               if (e.Row.Cells[_GRDLIST_STCUOTA].Text == ConstEstadoCuotaPresta.PENDIENTE)
                  e.Row.ForeColor = System.Drawing.Color.Green;
               else
               { 
                  e.Row.ForeColor = System.Drawing.Color.Blue;
                  bttedit.ImageUrl = "../Images/Icons/Disabled/Edit.png";
                  bttdelete.ImageUrl = "../Images/Icons/Disabled/Delete.png";
               }
         }

      }

      protected void bttOkPopup_Click(object sender, EventArgs e)
      {
         DataRow dr = null;
         dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);

         dr["idPlanilla"] = this.hdnidPlanillaPp.Value;
         dr["dsPlanilla"] = this.txtidPlanillaPp.Text;
         dr["idForPago"] = this.hdnidForPagoPp.Value;
         dr["dsForPago"] = this.txtidForPagoPp.Text;
         dr["nuProcAnual"] = this.txtnuProcAnualPp.Text;

         if (this.chkPagoEfectivoPp.Checked)
         {
            dr["feCancela"] = DateTime.Parse(this.txtfeCancelaPp.Text);
            dr["idTipoCance"] = ConstTipoPrestamo.PRESTAMO;
            dr["stCuota"] = ConstEstadoCuotaPresta.PAGADO;

            ETablaGenDet objEDet = new ETablaGenDet();
            MGBOQry.ITablaGen objBODet = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
            objEDet.IdTabla = ConstMultitabla.ESTADO_CUOTA_PRESTA;
            objEDet.IdCodigo = ConstEstadoCuotaPresta.PAGADO;
            objEDet = (ETablaGenDet)objBODet.GetDetailByKey(objEDet);
            if (objEDet != null)
               dr["dsStCuota"] = objEDet.DsDescrip;
            else
               dr["dsStCuota"] = "";

            objEDet.IdTabla = ConstMultitabla.TIPO_CANCEL_PRESTAMO;
            objEDet.IdCodigo = dr["idTipoCance"].ToString();
            objEDet = (ETablaGenDet)objBODet.GetDetailByKey(objEDet);
            if (objEDet != null)
               dr["dsTipoCance"] = objEDet.DsDescrip;
            else
               dr["dsTipoCance"] = "";
         }

         if (this.chkRecalcularPp.Checked)
         {
            int intNuProceMax = 0;//ultimo proceso de la planilla
            int intNuProc = int.Parse(this.txtnuProcAnualPp.Text);
            /*Obtiene ultimo Proceso de la planilla*/
            EPeriodoPlani objEP = new EPeriodoPlani();
            RHBOQry.IPeriodoPlani objBOP = (RHBOQry.IPeriodoPlani)WCFHelper.GetObject<RHBOQry.IPeriodoPlani>(typeof(RHBOQry.PeriodoPlani));
            objEP.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
            objEP.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objEP.IdForPago = this.hdnidForPagoPp.Value;
            objEP.IdPlanilla = this.hdnidPlanillaPp.Value;
            objEP.NuProcAnual = int.Parse(this.txtnuProcAnualPp.Text);
            objEP = (EPeriodoPlani)objBOP.GetByRowMax(objEP);
            if (objEP != null)
               intNuProceMax = objEP.NuProcAnual;
            else
               intNuProceMax = 0;
            /* the end*/



            foreach (DataRow dr2 in this.DataSource.Rows)
            {
               if (int.Parse(dr["nuCuota"].ToString()) <= int.Parse(dr2["nuCuota"].ToString()))
               {
                  if (intNuProceMax <= intNuProc)
                     intNuProc = 1;
                  dr2["nuProcAnual"] = intNuProc;
                  intNuProc++;
               }
            }
         }
         
         grdList.DataSource = this.DataSource;
         grdList.DataBind();

      }

      public override void LoadDataEditPopup(GridView grid, int rowIndex)
      {
         this.ClearEditPopup();

         DataRow dr = this.GridGetRowFromDataKey(this.DataSource, grdList, rowIndex);

         if (dr == null)
            return;

         WebHelper.RowToControl(dr, "nuCuota", this.txtnuCuotaPp);
         WebHelper.RowToControl(dr, "idPlanilla", this.hdnidPlanillaPp);
         WebHelper.RowToControl(dr, "dsPlanilla", this.txtidPlanillaPp);
         WebHelper.RowToControl(dr, "feCancela", this.txtfeCancelaPp);
         WebHelper.RowToControl(dr, "nuProcAnual", this.txtnuProcAnualPp);
         WebHelper.RowToControl(dr, "idForPago", this.hdnidForPagoPp);
         WebHelper.RowToControl(dr, "dsForPago", this.txtidForPagoPp);

      }

      public new void ClearEditPopup()
      {
         this.hdnidForPagoPp.Value = "";
         this.hdnidPlanillaPp.Value = "";
         this.txtidPlanillaPp.Text = "";
         this.txtidForPagoPp.Text = "";
         this.txtnuProcAnualPp.Text = "";
         this.txtfeCancelaPp.Text = "";
         
      }

      public override void SaveData(String key)
      {
         if (this.ValidateData() == false)
            return;

         IEntityBase objE = (IEntityBase)this.CreateEntitySet();
         IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Prestamo));

         String[] strRet = objPro.UpdateData(objE);
         String strMsg = "";

         switch (this.FormEditAction)
         {

            case FormEditAction.AddNew:
            case FormEditAction.Copy:
               strMsg = "Se ha registrado el Prestamo : " + strRet[0].ToString();
               break;
            default:
               strMsg = "Se ha modificado el Prestamo nro: " + this.txtidPrestamo.Text;
               break;
         }
         this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfPrestamoBnd.aspx");
      }

      public override void DeleteData()
      {
         IEntityBase objE = (IEntityBase)this.CreateEntitySet();
         IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));
         objPro.DeleteData(objE);

         //this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "Se anulo el derechohabiente  : " + txtidPersonal.Text, "wfPerDereHabiBnd.aspx");

      }

      public override Object CreateEntitySet()
      {
         EPrestamo objE = new EPrestamo();

         if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
         {
            objE.EntityState = EntityState.Added;
            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            WebHelper.ControlToEntity(hdnidPersonal, objE, "IdPersonal");
            WebHelper.ControlToEntity(txtidPrestamo, objE, "IdPrestamo");
            WebHelper.ControlToEntity(ddlidConRemu, objE, "IdConRemu");
            WebHelper.ControlToEntity(txtfePrestamo, objE, "FePrestamo");
            WebHelper.ControlToEntity(ddlidMoneda, objE, "IdMoneda");
            WebHelper.ControlToEntity(txtmtTC, objE, "MtTC");
            WebHelper.ControlToEntity(txtmtprestamo, objE, "MtPrestamo");
            WebHelper.ControlToEntity(txtqtcuotas, objE, "QtCuotas");
            WebHelper.ControlToEntity(hdnidPlanilla, objE, "IdPlanilla");
            WebHelper.ControlToEntity(hdnidForPago, objE, "IdForPago");
            WebHelper.ControlToEntity(txtnuProcAnual, objE, "NuProcAnual");
            WebHelper.ControlToEntity(ddlidTipoPrest, objE, "IdTipoPrest");
            WebHelper.ControlToEntity(txtdsObservacion, objE, "DsObserva");
         }
         else
         {
            IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Prestamo));
            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objE.IdPersonal = this.hdnidPersonal.Value;
            objE.IdPrestamo = this.txtidPrestamo.Text;
            objE = (EPrestamo)objBO.GetByKey(objE);

            if (this.FormEditAction == FormEditAction.Edit)
               objE.EntityState = EntityState.Modified;
            if (this.FormEditAction == FormEditAction.Delete)
               objE.EntityState = EntityState.Deleted;
            WebHelper.ControlToEntity(ddlidConRemu, objE, "IdConRemu");
            WebHelper.ControlToEntity(txtqtcuotas, objE, "QtCuotas");
            WebHelper.ControlToEntity(ddlidTipoPrest, objE, "IdTipoPrest");
            WebHelper.ControlToEntity(txtdsObservacion, objE, "DsObserva");



         }
         
         objE.StDocumento = ConstEstadoCuotaPresta.PENDIENTE;
         objE.StAnulado = ConstFlagEstado.DESACTIVADO;

         DataTable dt = (DataTable)this.DataSource;

         this.ClearDeletedRow(this.DataSource, "stAnulado");
         IList<EPrestaCuota> colECuota = new List<EPrestaCuota>();

         if (this.FormEditAction == FormEditAction.Delete)
         {
            foreach (DataRow dr in dt.Rows)
            {
               this.MarkDeletedRow(dr, "stAnulado");
            }
         }

         for (int intTypeUpd = 0; intTypeUpd <= 1; intTypeUpd++)
         {
            DataTable dtChange = null;
            if (intTypeUpd == 0)
               dtChange = dt.GetChanges(DataRowState.Added);
            else
               dtChange = dt.GetChanges(DataRowState.Modified);


            if (dtChange != null)
            {
               foreach (DataRow drDet in dtChange.Rows)
               {
                  EPrestaCuota objEDet = DataHelper.CopyDataRowToEntity<EPrestaCuota>(drDet, typeof(EPrestaCuota));

                  if (drDet.RowState == DataRowState.Added)
                     objEDet.EntityState = EntityState.Added;


                  if (drDet.RowState == DataRowState.Modified)
                  {
                     if (objEDet.StAnulado == ConstFlagEstado.DESACTIVADO)
                        objEDet.EntityState = EntityState.Modified;
                     else
                        objEDet.EntityState = EntityState.Deleted;
                  }

                  colECuota.Add(objEDet);
               }
            }
         }

         EPrestamoSet objESet = new EPrestamoSet();
         objESet.Prestamo = objE;
         objESet.PrestaCuota = colECuota;

         return objESet;
      }

      public override void LockControls()
      {
         if (this.FormEditAction != FormEditAction.AddNew)
         {
            this.txtmtprestamo.Enabled = false;
            this.txtidPersonal.EnabledButton = false;
            this.txtidPlanilla.EnabledButton = false;
            this.txtfePrestamo.Enabled = false;
            this.ddlidMoneda.Enabled = false;
            this.txtdsObservacion.Enabled = true;
         }
      }

      public void CalcularCuotas()
      {

         DataTable dt = (DataTable)this.DataSource;
         this.GridEditAction = GridEditAction.AddNew;

         int intPeriodo = int.Parse(Session[ConstSessionVar.PERIODO].ToString()); 
         int intICount = 0;//ultima cuota
         int intICountAnt = 0;//ultima cuota 

         decimal decMtTotal = 0;//total monto cancelado 
         int intNuProce = 0;//ultimo proceso
         int intNuProceMax = 0;//ultimo proceso de la planilla

         /*Obtiene ultimo Proceso de la planilla*/
         EPeriodoPlani objEP = new EPeriodoPlani();
         RHBOQry.IPeriodoPlani objBOP = (RHBOQry.IPeriodoPlani)WCFHelper.GetObject<RHBOQry.IPeriodoPlani>(typeof(RHBOQry.PeriodoPlani));
         objEP.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
         objEP.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objEP.IdForPago = this.hdnidForPago.Value;
         objEP.IdPlanilla = this.hdnidPlanilla.Value;
         objEP.NuProcAnual = int.Parse(this.txtnuProcAnual.Text);
         objEP = (EPeriodoPlani)objBOP.GetByRowMax(objEP);
         if (objEP != null)
            intNuProceMax = objEP.NuProcAnual;
         else
            intNuProceMax = 0;
         /* the end*/

         /*Elimina registros en estado pendiente*/
         DataRow[] drDel = dt.Select("stCuota = '" + ConstEstadoCuotaPresta.PENDIENTE + "'");
         if (drDel.Length > 0)
            for (int intA = 0; intA <= drDel.Length - 1; intA++)
               drDel[intA].Delete();
         dt.AcceptChanges();
         /*the end*/

         /*obtiene ultima cuota*/
         Object objResNuSec = null;
         objResNuSec = dt.Compute("MAX(nuCuota)", "");
         if (objResNuSec == null || objResNuSec.ToString() == "")
         {
            intICount = 0;
            intICountAnt = 0;
         }
         else
         {
            intICount = Convert.ToInt32(objResNuSec);
            intICountAnt = Convert.ToInt32(objResNuSec);
         }
         /*the end*/

         /*obtiene monto total cancelado*/
         objResNuSec = dt.Compute("SUM(mtCuota)", "");
         if (objResNuSec == null || objResNuSec.ToString() == "")
            decMtTotal = 0;
         else
            decMtTotal = Convert.ToDecimal(objResNuSec);
         /*the end*/

         /*Obtiene el proceso actual*/
         DataRow[] drintNuProce = dt.Select("nuCuota = '" + intICount + "'");
         if (drintNuProce.Length > 0)
         {
            intNuProce = int.Parse(drintNuProce[0]["nuProcAnual"].ToString()) + 1;
         }
         else
            intNuProce = int.Parse(this.txtnuProcAnual.Text);
         /*the end*/

         decimal decNuMtCuota = Math.Round((Decimal.Parse(this.txtmtprestamo.Text) - decMtTotal) / (Decimal.Parse(this.txtqtcuotas.Text) - intICount), 2);

         while (intICount < int.Parse(this.txtqtcuotas.Text))
         {
            DataRow dr = null;
            if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
            {
               dr = dt.NewRow();
               dr["nuCuota"] = intICount + 1;
               if (intNuProceMax < intNuProce)
               { intNuProce = 1;
               intPeriodo++;
               }
               dr["nuProcAnual"] = intNuProce;
               intNuProce++;
            }
            else
            {
               dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
            }

            if (this.GridEditAction != GridEditAction.Delete)
            {
               dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
               dr["idPersonal"] = this.hdnidPersonal.Value;
               dr["idPrestamo"] = Request.QueryString["idPrestamo"];

               if ((intICount + 1) == int.Parse(this.txtqtcuotas.Text))
               {
                  if ((decNuMtCuota * (int.Parse(this.txtqtcuotas.Text) - intICountAnt)) + decMtTotal > Math.Round(decimal.Parse(this.txtmtprestamo.Text),2))
                     dr["mtCuota"] = decNuMtCuota - ((decNuMtCuota * (int.Parse(this.txtqtcuotas.Text) - intICountAnt)) + (decMtTotal - Math.Round(decimal.Parse(this.txtmtprestamo.Text), 2)));
                  else
                     dr["mtCuota"] = decNuMtCuota + (Math.Round(decimal.Parse(this.txtmtprestamo.Text), 2) - ((decNuMtCuota * (int.Parse(this.txtqtcuotas.Text) - intICountAnt)) + decMtTotal));
               }
               else
                  dr["mtCuota"] = decNuMtCuota;

               dr["idPlanilla"] = this.hdnidPlanilla.Value;
               dr["dsPlanilla"] = this.txtidPlanilla.Text;
               dr["idForPago"] = this.hdnidForPago.Value;
               dr["dsForPago"] = this.txtidForPago.Text;
               dr["idPeriProc"] = intPeriodo.ToString();

               if (ddlidTipoPrest.SelectedItem.Value == ConstTipoPrestamo.ADELANTO)
                  dr["idTipoCance"] = ConstTipoPrestamo.ADELANTO;
               else
                  dr["idTipoCance"] = ConstTipoPrestamo.PRESTAMO;

               dr["stCuota"] = ConstEstadoCuotaPresta.PENDIENTE;

               ETablaGenDet objEDet = new ETablaGenDet();
               MGBOQry.ITablaGen objBODet = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
               objEDet.IdTabla = ConstMultitabla.ESTADO_CUOTA_PRESTA;
               objEDet.IdCodigo = ConstEstadoCuotaPresta.PENDIENTE;
               objEDet = (ETablaGenDet)objBODet.GetDetailByKey(objEDet);
               if (objEDet != null)
                  dr["dsStCuota"] = objEDet.DsDescrip;
               else
                  dr["dsStCuota"] = "";

               objEDet.IdTabla = ConstMultitabla.TIPO_CANCEL_PRESTAMO;
               objEDet.IdCodigo = dr["idTipoCance"].ToString();
               objEDet = (ETablaGenDet)objBODet.GetDetailByKey(objEDet);
               if (objEDet != null)
                  dr["dsTipoCance"] = objEDet.DsDescrip;
               else
                  dr["dsTipoCance"] = "";

               dr["stAnulado"] = ConstFlagEstado.DESACTIVADO;
            }
            if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
            {
               dt.Rows.Add(dr);
            }

            intICount++;


         }

         this.GridSetDataBinding(grdList, dt);
      }
   }
}